package xx.yy;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import java.util.Map;

/**
 * @author ljj
 * 2021/12/11 0011
 */
public class GameSDK {

    /**
     * 核心方法
     * @param params
     * //    Map<String,String> params = new HashMap<>();
     * //    params.put("event","showAD"); // 事件名称
     * //    params.put("k1","-1");        // 自定义值
     * //    params.put("k2","88");        // 自定义值
     */

    public static Object command(Map<String,Object> params){
        return invoke("command",new Class[]{Map.class},new Object[]{params});
    }

    public static void setGameSDKCallback(GameSDKCallback sdkCallback){
        invoke("setGameSDKCallback",new Class[]{GameSDKCallback.class},new Object[]{sdkCallback});
    }

    public static void applicationAttachBase(Context base){
        invoke("applicationAttachBase",new Class[]{Context.class},new Object[]{base});
    }

    public static void applicationOnCreate(Application application){
        invoke("applicationOnCreate",new Class[]{Application.class},new Object[]{application});
    }

    public static void init(Activity activity,Bundle savedInstanceState) {
        invoke("init",new Class[]{Activity.class,Bundle.class},new Object[]{activity,savedInstanceState});
    }

    public static void onStop() {
        invoke("onStop");
    }

    public static void onStart() {
        invoke("onStart");
    }

    public static void onRestart() {
        invoke("onRestart");
    }

    public static void onResume() {
        invoke("onResume");
    }

    public static void onPause() {
        invoke("onPause");
    }
    
    public static void onDestroy() {
        invoke("onDestroy");
    }

    public static void onActivityResult(int requestCode, int resultCode, Intent data) {
        invoke("onActivityResult",new Class[]{int.class,int.class,Intent.class},new Object[]{requestCode,resultCode,data});
    }

    public static void onNewIntent(Intent intent) {
        invoke("onNewIntent",new Class[]{Intent.class},new Object[]{intent});
    }

    public static void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults) {
        invoke("onRequestPermissionsResult",new Class[]{int.class,String[].class,int[].class},new Object[]{requestCode,permissions,grantResults});
    }

    // 预留
    public static void ext(Object o){
        invoke("ext",new Class[]{Object.class},new Object[]{o});
    }

    /** =======================================================================================================================**/

    public interface GameSDKCallback{
        void onResult(Map<String,Object> result);
    }

    private static Class sdkClass =null;

    private static Object invoke(String methodName){
        return invoke(methodName,null,null);
    }

    private static Object invoke(String methodName,  Class[] paramsClasses,Object[] params){
        try {
            if (sdkClass == null){
                sdkClass =  Class.forName("xx.yy.YouySDK");
            }
            if (paramsClasses == null){
               return sdkClass.getMethod(methodName).invoke(null);
            }
            return sdkClass.getMethod(methodName,paramsClasses).invoke(null , params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
